/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zxg.zbc.web.common.module;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.citrus.service.requestcontext.parser.ParameterParser;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alipay.util.AlipayNotify;

/**
 *
 * @author Hangding
 */
public abstract class BasePayResult {
  final static Logger log = LoggerFactory.getLogger(BasePayResult.class);


  public void execute(Context context, Navigator nav, ParameterParser param) {
    try {
      // 商户订单号

      String out_trade_no = param.getString("out_trade_no");

      // 支付宝交易号

      String trade_no = param.getString("trade_no");

      // 交易状态
      String trade_status = param.getString("trade_status");

      String buyer_email = param.getString("buyer_email");

      String totalFee = param.getString("total_fee");
      // 获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
      Map<String, String> params = new HashMap<String, String>();
      for (Iterator<String> iter = param.keySet().iterator(); iter.hasNext();) {
        String name = (String) iter.next();
        String[] values = param.getStrings(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
          valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
        }
        // 乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
        params.put(name, valueStr);
      }
      // 计算得出通知验证结果
      boolean verify_result = AlipayNotify.verify(params);
      // verify_result = true;//0712暂时跳过验证
      if (verify_result) {// 验证成功
        //////////////////////////////////////////////////////////////////////////////////////////
        // 请在这里加上商户的业务逻辑程序代码
        // ——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
        if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {
          // 判断该笔订单是否在商户网站中已经做过处理
          // 如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
          // 如果有做过处理，不执行商户的业务程序
          processTradeSuccess(out_trade_no, trade_no, trade_status, buyer_email, totalFee, nav);

        }

      } else {
        processTradeError(out_trade_no, trade_no, trade_status, buyer_email, totalFee, nav);
      }
    } catch (Exception e) {
      log.error("error process pay", e);
    }

  }

  protected abstract void processTradeError(String out_trade_no, String trade_no, String trade_status, String buyer_email, String totalFee, Navigator nav);

  protected abstract void processTradeSuccess(String out_trade_no, String trade_no, String trade_status, String buyer_email, String totalFee, Navigator nav);
}
